<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use QL\QueryList;

class Chuli extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('Chuli')
            ->setDescription('the chuli command');
    }

    protected function execute(Input $input, Output $output)
    {

        $url = 'https://c.runoob.com/';
        // 元数据采集规则
        $rules = [
            // 采集文章标题
            'title' => ['.runoob-card-tit','text'],
            'url' => ['a','href'],
            'descs' => ['.runoob-card-des','text'],
            'pic' => ['.runoob-card-tit>img','src'],
        ];
        // 切片选择器
        $range = '#runoob-goto-15 .col-md-12 .row .runoob-padding';
        $rt = QueryList::get($url)->rules($rules)->range($range)->query()->getData();
        // $list = $rt->all();
        $arr = [];
        foreach ($rt->all() as $key => $value) {
            $arr[$key]['title'] = trim($value['title']);
            $arr[$key]['url']   = trim($value['url']);
            $arr[$key]['descs'] = trim($value['descs']);
            $arr[$key]['pic']   = trim($value['pic']);
            $arr[$key]['create_time']   = time();
            $arr[$key]['update_time']   = time();
            $arr[$key]['type_id']       = '13';
        }


        $res = Db::table('ea_nav_site')->insertAll($arr);
        dump($arr, $res);


        // $this->runoob();
        // dump(Db::table('ea_ip')->count());exit;
        // $k_time = microtime(true);
        // try {
        //     Db::startTrans();
        //     for ($i=1; $i <= 8; $i++) {
        //         $url = 'https://www.jb51.net/books/list246_'.$i.'.html';
        //         $rules = [
        //             'title' => ['.tit>a','text'],
        //             'link' => ['.tit>a','href'],
        //             'size' => ['.other>span:eq(1)','text'],
        //             'create_time' => ['.other>span:eq(2)','text'],
        //         ];
        //         // 切片选择器,跳过第一条广告
        //         $range = '#list_ul_more li';
        //         $rt = QueryList::get($url)->rules($rules)->range($range)->query()->getData();
        //         // print_r($rt->all());

        //         $arr = [];

        //         foreach ($rt->all() as $key => $value) {
        //             $arr['title'] = $value['title'];
        //             $arr['size'] = str_replace('大小：', '', $value['size']);

        //             // 获取详细
        //             $url = 'https://www.jb51.net'.$value['link'];
        //             $rules = [
        //                 'content' => ['#soft-intro','html'],
        //                 'pub-time' => ['.pub-time','text'],
        //                 'img' => ['.softatlas>ul>li:eq(0)>img', 'src'],
        //                 'downloads_links' => ['.ul_Address>li:eq(0)>a', 'href'],
        //             ];

        //             $ql = QueryList::get($url)->rules($rules)->query()->getData(function($item){
        //                 $ql = QueryList::html($item['content']);
        //                 $ql->find('#downintro1,.soft_picc')->remove();
        //                 $item['content'] = $ql->find('')->html();
        //                 return $item;
        //             });

        //             if($ql['downloads_links']){
        //                 $arr['downloads_links']    = $ql['downloads_links'];
        //             }

        //             $arr['pre_id'] = '5';
        //             $arr['som_id'] = '24';
        //             $arr['tag_link']    = $url;
        //             $arr['img']    = $ql['img'];
        //             $arr['content'] = $ql['content'];
        //             $arr['create_time'] = strtotime(str_replace('更新时间：', '', $ql['pub-time']));
        //             dump($i.'==='.$key);
        //             Db::table('ea_book')->insert($arr);
        //         }
                
        //     }

        //     // 提交事务
        //     Db::commit();
        //     dump('End，结束, 总执行时间为：'.round((microtime(true)-$k_time), 3).' s');

        // } catch (\Exception $e) {
            
        //     Db::rollback();

        //     dump(date('Y-m-d H:i:s').' ==> 异常:'.$e->getMessage());
        // }
        

        // for ($i=1; $i <= 30; $i++) { 
            
        //     $url = 'https://proxy.ip3366.net/free/?action=china&page='.$i;

        //      $table = QueryList::get($url)->find('table');

        //     // dump($data);exit;
        //     // 元数据采集规则
        //     $tableHeader = $table->find('tr:eq(0)')->find('td')->texts();
        //     // 采集表的每行内容
        //     $tableRows = $table->find('tr:gt(0)')->map(function($row){
        //         return $row->find('td')->texts()->all();
        //     });

        //     $arr = [];
        //     $list = $tableRows->all();
        //     // dump($list);exit;

        //     foreach ($list as $key => $value) {
        //         $arr[$key]['ip'] = $value[0];
        //         $arr[$key]['port'] = $value[1];
        //         $arr[$key]['desc'] = $value[2];
        //         $arr[$key]['type'] = $value[3];
        //         $arr[$key]['addr'] = $value[4];
        //         $arr[$key]['ck_time'] = $value[5];
        //         $arr[$key]['speed'] = $value[6];
        //         $arr[$key]['create_time'] = time();
        //     }
            
        //     Db::table('ea_ip')->insertAll($arr);
        //     dump($i);

        // }

        // exit;


        // for ($i=1; $i <= 10; $i++) { 
            
        //     $url = 'https://www.kuaidaili.com/free/intr/5'.$i;

        //     // $table = QueryList::get($url,null,[
        //     //     'proxy' => 'http://106.54.128.253:999',
        //     //     'headers' => [
        //     //         'Referer' => 'https://www.baidu.com/',
        //     //         'User-Agent' => 'User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.5',
        //     //         'Accept'     => 'application/json',
        //     //         'X-Foo'      => ['Bar', 'Baz'],
        //     //         // 携带cookie
        //     //         'Cookie'    => 'abc=111;xxx=222'
        //     //     ]
        //     // ])->find('table');
        //      $table = QueryList::get($url)->find('table');

        //     // dump($data);exit;
        //     // 元数据采集规则
        //     $tableHeader = $table->find('tr:eq(0)')->find('td')->texts();
        //     // 采集表的每行内容
        //     $tableRows = $table->find('tr:gt(0)')->map(function($row){
        //         return $row->find('td')->texts()->all();
        //     });

        //     $arr = [];
        //     $list = $tableRows->all();

        //     foreach ($list as $key => $value) {
        //         $arr[$key]['ip'] = $value[0];
        //         $arr[$key]['port'] = $value[1];
        //         $arr[$key]['desc'] = $value[2];
        //         $arr[$key]['type'] = $value[3];
        //         $arr[$key]['addr'] = $value[4];
        //         $arr[$key]['ck_time'] = $value[5];
        //         $arr[$key]['speed'] = $value[6];
        //         $arr[$key]['create_time'] = time();
        //     }
            
        //     Db::table('ea_ip')->insertAll($arr);
        //     dump($i);
        //     sleep(2);

        // }
        // dump('End');


        // $list = Db::table('ea_project_open_source')->select();

        // foreach ($list as $key => $value) {
        //     $create_time = '2022-03-'.rand(1, 30).' '.rand(7, 23).':'.rand(1, 59).':'.rand(1, 59);
        //     Db::table('ea_project_open_source')->where('id', $value['id'])->update([
        //         'create_time' => strtotime($create_time), //rand(1647392424, 1648612824)
        //     ]);

        //     dump($key);
        // }
    }

    public function runoob()
    {
        $url = 'https://c.runoob.com/';
        // 元数据采集规则
        $rules = [
            // 采集文章标题
            'title' => ['.runoob-card-tit','text'],
            'url' => ['.runoob-padding>a','href'],
            'descs' => ['.runoob-card-des','text'],
            'pic' => ['.runoob-card-tit?img','src'],
        ];
        // 切片选择器
        $range = '#runoob-goto-37 .col-md-12 .row';
        $rt = QueryList::get($url)->rules($rules)->range($range)->query()->getData();
        // $list = $rt->all();
        dump($rt->all());

        // foreach ($list as $key => $value) {
        //     $value['type_id'] = '11';
        //     $value['create_time'] = time();
        //     $value['update_time'] = time();
        //     $value['link'] = str_replace('/periodical/statistics/click/?target=', '', $value['link']);
        //     array_push($arr, $value);
        // }
    }

    public function pac()
    {
        $arr = [];

        for ($i=1; $i <= 17; $i++) { 
            // code...
            $url = 'https://www.hellogithub.com/periodical/category/Python%20%E9%A1%B9%E7%9B%AE/?page='.$i;
            // 元数据采集规则
            $rules = [
                // 采集文章标题
                'title' => ['.project-url','text'],
                // 采集链接
                'link' => ['.project-url','href'],

                // 采集缩略图
                // 采集文档简介
                // 'descs' => ['.description','text']
            ];
            // 切片选择器
            $range = '.content .content-subhead';
            $rt = QueryList::get($url)->rules($rules)->range($range)->query()->getData();
            $list = $rt->all();

            foreach ($list as $key => $value) {
                $value['type_id'] = '11';
                $value['create_time'] = time();
                $value['update_time'] = time();
                $value['link'] = str_replace('/periodical/statistics/click/?target=', '', $value['link']);
                array_push($arr, $value);
            }

        }

        $res = Db::table('ea_project_open_source')->insertAll($arr);
        dump($res);exit;
    }

    /**
     * [ip 获取免费的代理IP]
     * @since  2022-03-30
     * @author Dai哥
     * @return [type]     [https://www.kuaidaili.com/free/inha/30/]
     */
    public function ip()
    {
        $arr = [];
        $k_time = microtime(true);

        for ($i=1; $i <= 4532; $i++) { 
            
            $url = 'https://www.kuaidaili.com/free/inha/'.$i;

            $table = QueryList::get($url)->find('table');
            // dump($data);exit;
            // 元数据采集规则
            $tableHeader = $table->find('tr:eq(0)')->find('td')->texts();
            // 采集表的每行内容
            $tableRows = $table->find('tr:gt(0)')->map(function($row){
                return $row->find('td')->texts()->all();
            });

            Db::table('')->insertAll($tableRows->all());
            dump($i);
        }


        dump('End');
        CliEcho::notice(date('Y-m-d H:i:s').' ==> 结束, 总执行时间为：'.round((microtime(true)-$k_time), 3).' s');
    }
}
